﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Ymatou.ServiceCenter.Domain.IRepositories;
using Ymatou.ServiceCenter.Domain.IServices;
using Ymatou.ServiceCenter.Domain.Models;

namespace Ymatou.ServiceCenter.Repository
{
    public class ServiceDependencyRepository : RepositoryBase<ServiceDependency>, IServiceDependencyRepository
    {
        public PagingInfo<ServiceDependency> GetServiceDependency(string consumerAppId, string providerAppId, int pageSize, int pageIndex)
        {
            DynamicParameters dynamicParameters = new DynamicParameters();
            StringBuilder conditionBuilder = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(consumerAppId))
            {
                conditionBuilder.Append(" And ConsumerAppId=@ConsumerAppId");
                dynamicParameters.Add("ConsumerAppId", consumerAppId);
            }
            if (!string.IsNullOrWhiteSpace(providerAppId))
            {
                conditionBuilder.Append(" And ProviderAppId=@ProviderAppId");
                dynamicParameters.Add("ProviderAppId", providerAppId);
            }
            string condition = conditionBuilder.ToString().TrimStart(" And".ToCharArray());
            return base.PagingQuery(null, "servicedependency", condition, null, dynamicParameters, pageSize, pageIndex);
        }

        public IList<ServiceDependency> MyConsumerList(string appId)
        {
            if (string.IsNullOrWhiteSpace(appId))
                throw new ArgumentNullException("appId");
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("ProviderAppId", appId);
            return base.GetList(null, "servicedependency", "ProviderAppId=@ProviderAppId", dynamicParameters);
        }

        public IList<ServiceDependency> MyProviderList(string appId)
        {
            if (string.IsNullOrWhiteSpace(appId))
                throw new ArgumentNullException("appId");
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("ConsumerAppId", appId);
            return base.GetList(null, "servicedependency", "ConsumerAppId=@ConsumerAppId", dynamicParameters);
        }
    }
}
